#include "led.h"
#include "s3c24xx.h"


static LED leds[4] = {{1, 5, 0}, {2, 6, 0}, {3, 7, 0}, {4, 8, 0}};

void handle_led(int id, int on) {
	LED led = leds[id-1];
	if(on) {
		GPBDAT &= ~(1<<led.gpio);
        // puts("on\n\r");
	} else {
		GPBDAT |= (1<<led.gpio);
        // puts("off\n\r");
	}
}

static void led_all_off() {
	handle_led(1, 0);
	handle_led(2, 0);
	handle_led(3, 0);
	handle_led(4, 0);
}

void led_init() {
	/* 设置GPFCON让GPB 5/6/7/8配置为输出引脚 */
	// GPBCON &= ~((3<<10) | (3<<12) | (3<<14) | (3<<16));
	// GPBCON |=  ((1<<10) | (1<<12) | (1<<14) | (1<<16));

	GPBCON &= ~((3<<(leds[0].gpio*2)) | (3<<(leds[1].gpio*2)) | 
		(3<<(leds[2].gpio*2)) | (3<<(leds[3].gpio*2)));

	GPBCON |=  ((1<<(leds[0].gpio*2)) | (1<<(leds[1].gpio*2)) | 
		(1<<(leds[2].gpio*2)) | (1<<(leds[3].gpio*2)));

    led_all_off();
}

void delay(volatile int d) {
	while (d--);
}

void test_led() {
    led_init();
    int i = 1;
    while (1) {
        if(i >= 5) {
            i = 1;
        }
        handle_led(i, 1);
        delay(500000);
        led_all_off();
        i++;
    }
}
